﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using System.Text;

namespace Actuals.Data
{
    public class DalRelease : Dal<IRelease, Release>, IDalRelease 
    {

         private readonly IDalManager _manager;
        private static readonly IDalRelease _instance = new DalRelease();
        private const string SP_GET_Release = "usp_GetRelease";
        private const string SP_GET_RELEASES  = "usp_GetReleases";
        private const string SP_ADD_RELEASE = "usp_Add_Release";
        private const string SP_UPDATE_RELEASE = "usp_UpdateRelease";
        private const string SP_DELETE_RELEASE = "usp_DeleteRelease";


            
        public static IDalRelease Instance
        {
            get { return _instance; }
        }

        public DalRelease()
        {
            _manager = new DalManager();
        }

        public DalRelease(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IRelease obj, DataRow row)
        {
            obj.ProjectId = _manager.GetValue<int>(row, "ProjectId");
            obj.ReleaseId = _manager.GetValue<int>(row, "ReleaseId");
            obj.Description = _manager.GetValue<string>(row, "Description");
            obj.StartDate = _manager.GetValue<DateTime>(row, "StartDate");
            obj.EndDate = _manager.GetValue<DateTime>(row, "EndDate");
            obj.TeamId = _manager.GetValue<int>(row, "TeamId");
        }
        public IRelease GetRelease(int projectId, int releaseId)
        {
            _manager.StoredProcedureName = SP_GET_Release;
            _manager.AddInParameter("@ProjectId", DbType.Int32 , projectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, releaseId);

            IRelease release = Convert(_manager.ExecuteDataSet());


            return release;
        }

        public IList<IRelease> GetReleases(int projectId)
        {
            _manager.StoredProcedureName = SP_GET_RELEASES;
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);

            List<IRelease> releases = new List<IRelease>();
            foreach (IRelease release  in ConvertAll(_manager.ExecuteDataSet()))
                releases.Add(release);

            return releases;

           
        }
        public bool AddRelease(IRelease release)
        {
            _manager.StoredProcedureName = SP_ADD_RELEASE;
            _manager.AddInParameter("@ProjectId",DbType.Int32,release.ProjectId);
            _manager.AddInParameter("@Description", DbType.String, release.Description);
            _manager.AddInParameter("@StartDate", DbType.DateTime, release.StartDate);
            _manager.AddInParameter("@EndDate", DbType.DateTime, release.EndDate);
            _manager.AddInParameter("@TeamId", DbType.Int32, release.TeamId);
            _manager.ExecuteNonQuery();
            return true;
        }

        public bool UpdateRelease(IRelease release)
        {
            _manager.StoredProcedureName = SP_UPDATE_RELEASE;
            _manager.AddInParameter("@ProjectId", DbType.Int32, release.ProjectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, release.ReleaseId);
            _manager.AddInParameter("@Description", DbType.String, release.Description);
            _manager.AddInParameter("@StartDate", DbType.DateTime, release.StartDate);
            _manager.AddInParameter("@EndDate", DbType.DateTime, release.EndDate);
            _manager.AddInParameter("@TeamId", DbType.Int32, release.TeamId);
            _manager.ExecuteNonQuery();
            return true;

        }

        public bool DeleteRelease(IRelease release)
        {
            _manager.StoredProcedureName = SP_DELETE_RELEASE;
            _manager.AddInParameter("@ProjectId", DbType.Int32, release.ProjectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, release.ReleaseId);

            _manager.ExecuteNonQuery();

            return true;
        }


    }
}
